#! python
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name:        simpleconfigparser
# Purpose:     parse Microsoft Windows INI like files with a single line
#              parser based on configparser
#              Module.
#              simpleConfigParser will return a class with given configfilename.
#
# Author:      Xu Bin
#
# Created:     12/03/2014
# Copyright:   (c) Xu Bin 2014
# Licence:     BSD

# USAGE:
#
# simpleconfigparser can read configure files like following one:
# (Notes that the types are specified in typedef section, and the section names
# are ignored.)
# ############## start of example.ini ##################
# [test]
# intexample = 45
# boolexample = yes
# stringexample = i am a string
# hexexample = 0xA7
#
# [typedef]
# intexample = int
# boolexample = bool
# stringexample = str
# hexexample = hex
# ############## end of example.ini ##################
# first we have to import simpleconfigparser
# >>> from simpleconfigparser import simpleconfigparser
#
# to read the example.ini file, simply use
# >>> cfg = simpleconfigparser('example.ini')
#
# cfg is a class and all variables are in it
# >>> dir(cfg)
# ['__doc__',
#  '__module__',
#  'boolexample',
#  'hexample',
#  'intexample',
#  'stringexample']
# >>> cfg.boolexample
# True
# >>> cfg.hexample
# 167
# >>> cfg.intexample
# 45
# >>> cfg.stringexample
# 'i am a string'


#-------------------------------------------------------------------------------

def _str2bool(v):
    return v.lower() in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly',
                       'uh-huh']
def _str2hex(v):
    return int(v,16)

_typeconvtable = {'int':int,
                  'float':float,
                  'hex':_str2hex,
                  'bool':_str2bool,
                  'string':str,
                  'str':str,
                  }
def simpleconfigparser(configfilename,typedefsectionname='typedef'):
    '''function simpleConfigParser will return a class with given configfilename.
It will NOT seperate sections.
It use section typedefsectionname("typedef") to specify the types of the values.
'''
    class config():
        pass
    import configparser
    raw_config = configparser.ConfigParser()
    raw_config.read(configfilename)
    typedef = {}
    if not typedefsectionname in raw_config:
        print '"',typedefsectionname,'" section no found in configurefile. No type convertions'
    else:
        for (valname,val) in raw_config[typedefsectionname].items():
            if val in _typeconvtable:
                typedef[valname] = _typeconvtable[val]
            else:
                print 'type "',val,'" is not supported'
    for sectionname in raw_config.sections():
        if sectionname == typedefsectionname:
            continue
        for (valname,val) in raw_config[sectionname].items():
            setattr(config,valname,val)
        for (valname,valtype) in typedef.items():
            setattr(config,valname,valtype(raw_config[sectionname][valname]))
    return config
